﻿Imports System.Data.SqlClient

Module MainModule

    Sub Main()

        Dim myConnection As SqlConnection = StartupParameter.DBConnection

        Dim myTableInfos As Dictionary(Of String, TableInfo) = ReadTables()

        CreateViews(myTableInfos)

        UpdateTableInfoWithViews(myTableInfos)

        'close without error
        System.Environment.Exit(0)

    End Sub

    Private Function ReadTables() As Dictionary(Of String, TableInfo)

        Dim myReaderTable As SqlDataReader = GetSQLDatareader(SQLScripts.GetAllTables)
        Dim myTableInfos As New Dictionary(Of String, TableInfo)
        While myReaderTable.Read()
            Dim myTableName As String = GetString(myReaderTable, SQLScripts.Table.cTABLE_NAME_Column_ID)
            Dim myTableInfo As New TableInfo With {.TableName = myTableName}
            myTableInfos.Add(myTableName.ToLowerInvariant, myTableInfo)
        End While
        myReaderTable.Close()

        Dim myReaderFieldInfo As SqlDataReader = GetSQLDatareader(SQLScripts.GetAllFields())
        While myReaderFieldInfo.Read()
            Dim myTablename As String = myReaderFieldInfo.GetString(SQLScripts.Fields.cTABLE_NAME_Column_ID)
            Dim myFieldname As String = myReaderFieldInfo.GetString(SQLScripts.Fields.cCOLUMN_NAME_Column_ID)
            Dim myFieldInfo As New FieldInfo With {.FieldName = myFieldname, _
                                                   .CharacterMaxLength = GetInteger(myReaderFieldInfo, SQLScripts.Fields.cCHARACTER_MAXIMUM_LENGTH_Column_ID), _
                                                   .CharacterSet = GetString(myReaderFieldInfo, SQLScripts.Fields.cCHARACTER_SET_NAME_Column_ID), _
                                                   .CollationName = GetString(myReaderFieldInfo, SQLScripts.Fields.cCOLLATION_NAME_Column_ID), _
                                                   .DataType = GetString(myReaderFieldInfo, SQLScripts.Fields.cDATA_TYPE_Column_ID), _
                                                   .IsNullable = GetBoolean(myReaderFieldInfo, SQLScripts.Fields.cIS_NULLABLE_Column_ID), _
                                                   .NumericPrecision = GetInteger(myReaderFieldInfo, SQLScripts.Fields.cNUMERIC_PRECISION_Column_ID), _
                                                   .OrdinalPosition = GetInteger(myReaderFieldInfo, SQLScripts.Fields.cORDINAL_POSITION_Column_ID)}
            'find the right table
            If myTableInfos.ContainsKey(myTablename.ToLowerInvariant) Then
                Dim myTableInfo As TableInfo = myTableInfos.Item(myTablename.ToLowerInvariant)
                myTableInfo.FieldInfos.Add(myFieldname.ToLowerInvariant, myFieldInfo)
            Else
                'we ignore this table as it could be a view
                'ConsoleAnswers.WriteErrorMessage(String.Format("Table {0} not found", myTablename))
            End If
        End While
        myReaderFieldInfo.Close()

        Dim myForeignKeys As New Dictionary(Of String, ForeignKeyInfo)
        Dim myReaderForeignKey As SqlDataReader = GetSQLDatareader(SQLScripts.GetAllForeignKeys())
        While myReaderForeignKey.Read()
            Dim myTablename As String = GetString(myReaderForeignKey, SQLScripts.ForeignKey.cForeignKey_TableName_Column_ID)
            Dim myFieldname As String = GetString(myReaderForeignKey, SQLScripts.ForeignKey.cForeignKey_ColumnName_Column_ID)
            Dim myPrimaryTablename As String = GetString(myReaderForeignKey, SQLScripts.ForeignKey.cPrimaryKey_TableName_Column_ID)

            Dim myForeignKeyInfo As New ForeignKeyInfo With {.PrimaryKey_TableName = myPrimaryTablename, _
                                                             .PrimaryKey_ColumnName = myFieldname}

            'find the right table
            If myTableInfos.ContainsKey(myTablename.ToLowerInvariant) Then
                Dim myTableInfo As TableInfo = myTableInfos.Item(myTablename.ToLowerInvariant)
                myTableInfos.TryGetValue(myTablename.ToLowerInvariant, myTableInfo)
                'now we find the fields
                Dim myFieldInfos As Dictionary(Of String, FieldInfo) = myTableInfo.FieldInfos
                If myFieldInfos.ContainsKey(myFieldname.ToLowerInvariant) Then
                    Dim myFieldInfo As FieldInfo = myFieldInfos.Item(myFieldname.ToLowerInvariant)
                    myFieldInfo.ForeignKeyInfos.Add(myPrimaryTablename.ToLowerInvariant, myForeignKeyInfo)
                Else
                    ConsoleAnswers.WriteErrorMessage(String.Format("Field {0} for Table {1} not found ", myFieldname, myTablename))
                End If
            Else
                'we ignore this table as it could be a view
                'ConsoleAnswers.WriteErrorMessage(String.Format("Table {0} not found", myTablename))
            End If

        End While
        myReaderForeignKey.Close()

        Return myTableInfos
    End Function

    Private Function UpdateTableInfoWithViews(ByVal aTableInfos As Dictionary(Of String, TableInfo)) As Dictionary(Of String, TableInfo)
        'fasd
    End Function

    Private Sub CreateViews(ByVal aTableInfos As Dictionary(Of String, TableInfo))
        Dim myViews As New List(Of String)

        Dim myReaderView As SqlDataReader = GetSQLDatareader(SQLScripts.GetAllViews)
        Dim myTableInfos As New Dictionary(Of String, TableInfo)
        While myReaderView.Read()
            Dim myViewName As String = GetString(myReaderView, SQLScripts.Views.cViewName_Column_ID).ToLowerInvariant
            myViews.Add(myViewName.ToLowerInvariant)
        End While
        myReaderView.Close()

        For Each myTableInfo As TableInfo In aTableInfos.Values
            Dim myViewName As String = myTableInfo.ViewName.ToLowerInvariant
            'check if view exist
            If Not myViews.Contains(myViewName) Then
                'create view
                Dim mySQL As String = SQLScripts.GetWriteView(aTableInfos:=aTableInfos, aTableInfo:=myTableInfo)
                Dim myCmd As New SqlCommand(mySQL, StartupParameter.DBConnection)
                Try
                    myCmd.ExecuteNonQuery()
                Catch ex As Exception
                    ConsoleAnswers.WriteErrorMessage(ex.Message)
                    ConsoleAnswers.WriteMessage(mySQL)
                End Try
            End If
        Next

    End Sub

    Private Function GetSQLDatareader(ByVal aSQLScript As String) As SqlDataReader
        Dim myCMD As New SqlCommand(aSQLScript, StartupParameter.DBConnection)
        Dim myReader As SqlDataReader = myCMD.ExecuteReader()
        Return myReader
    End Function

    Private Function GetString(ByVal aDataReader As SqlDataReader, ByVal aColumnIndex As Integer) As String
        Dim myReturn As String = String.Empty
        Try
            myReturn = aDataReader.GetString(aColumnIndex)
        Catch ex As Exception
            Return myReturn
        End Try
        Return myReturn
    End Function

    Private Function GetBoolean(ByVal aDataReader As SqlDataReader, ByVal aColumnIndex As Integer) As Boolean
        Dim myReturn As Boolean = False
        Try
            myReturn = aDataReader.GetBoolean(aColumnIndex)
        Catch ex As Exception
            Return myReturn
        End Try
        Return myReturn
    End Function

    Private Function GetInteger(ByVal aDataReader As SqlDataReader, ByVal aColumnIndex As Integer) As Integer
        Dim myReturn As Integer = 0
        Try
            myReturn = aDataReader.GetInt32(aColumnIndex)
        Catch ex As Exception
            Return myReturn
        End Try
        Return myReturn
    End Function

    'Private Sub DoTheWork()


    '    'Create an SQLServer-instance
    '    Dim myApplicationDB As ISQLServer = Nothing
    '    Try
    '        myApplicationDB = ApplicationSelector.ApplicationDB
    '    Catch ex As Exception
    '        MsgBox("Error connecting to the SQL-server! The message was: " & ex.Message)
    '        Return
    '    End Try
    '    'Get UI-Languages 
    '    Dim myMasterUILangIDs As Int32() = Nothing
    '    Try
    '        myMasterUILangIDs = GetUILanguages(myMasterDB)
    '    Catch ex As Exception
    '        MsgBox("Error reading the userinterface-languages. The message was: " & ex.Message)
    '        Return
    '    End Try
    '    'Drop all autogenerated views (from the application-db)
    '    If (chkRecreateViews.Checked) Then
    '        'Declare commonly used objects
    '        Dim myDTOInfosByView As SortedDictionary(Of String, DataRow) = InitDTOInfoByViewName(myApplicationId, myMasterDB)

    '        mySelect = New SQL_Select("sysobjects")
    '        mySelect.Distinct = True
    '        mySelect.Column("name", "xtype")
    '        myWhere1 = New SQL_Where("xtype", "v")
    '        myWhere2 = SQL_Where.ColumnStartsWith("name", "zzz_")
    '        mySelect.WhereAndWhere(myWhere1, myWhere2)
    '        myDT = myApplicationDB.Exec2DataTable(mySelect)
    '        For Each myRow As DataRow In myDT.Rows
    '            Dim myTableName As String = myTranslator.ToString(myRow.Item("name"))
    '            'find the DTOInfo
    '            Dim myDTOInfoAutoMaintained As Boolean = True
    '            If myDTOInfosByView.ContainsKey(myTableName) Then
    '                myDTOInfoAutoMaintained = myTranslator.ToBoolean(myDTOInfosByView.Item(myTableName).Item("DTOInfo_AutoMaintained"))
    '            End If
    '            'TODO: Only drop the not used views
    '            If (Not myDTOInfoAutoMaintained) Then Continue For
    '            Try
    '                WriteInfo(String.Format("Dropping view {0}", myTableName))
    '                Dim myDropView As New SQL_DropView(myTableName)
    '                myApplicationDB.Exec(myDropView)
    '            Catch ex As Exception
    '                MsgBox(String.Format("Error deleting the auto-generated view ""{0}"" from the application-db!. The message was: {1}", myTableName, ex.Message))
    '                Return
    '            End Try
    '        Next
    '    End If
    '    'Get the currently existing DTO-views (from the application-db)
    '    mySelect = New SQL_Select("sysobjects")
    '    mySelect.Distinct = True
    '    mySelect.Column("name")
    '    myWhere1 = New SQL_Where("xtype", "v")
    '    myWhere2 = SQL_Where.ColumnStartsWith("name", "zzz_")
    '    mySelect.WhereAndWhere(myWhere1, myWhere2)
    '    Dim myExistingViewNames As New Dictionary(Of String, String)(myComparer)
    '    For Each myViewName As String In myApplicationDB.Exec2StringArray(mySelect)
    '        myExistingViewNames.Add(myViewName, myViewName)
    '    Next
    '    'Synchronize the DTOInfos (on the master-db)
    '    Try
    '        SynchronizeDTOInfoEntries(myApplicationId, myDTONamespace, myApplicationDB, myMasterDB)
    '    Catch ex As Exception
    '        MsgBox("Error synchronizing the DTOInfo of the master database with the table/view information of the application database! The message was: " & ex.Message)
    '        Return
    '    End Try
    '    'Create DTOInfoTranslation entries (on the master-db)
    '    Try
    '        WriteInfo("Creating missing localized object entries")
    '        Dim myNative As String = "INSERT DTOInfoTranslation (DTOInfo.DTOInfo_ID, TranslationLang_.LanguageIAF_ID, DTOInfo_Label, DTOInfo_Description, DTOInfo_ImageName) SELECT DTOInfo.DTOInfo_ID, TranslationLang_.LanguageIAF_ID, '[' + RTRIM(SUBSTRING(DTOInfo.DTOInfo_DTOBaseName,1,48)) + ']' as DTOInfo_Label, '[DTOInfo for ' + DTOInfo.DTOInfo_DTOBaseName + ']' as DTOInfo_Description, DTOInfo.DTOInfo_DTOBaseName + '.png' as DTOInfo_ImageName FROM DTOInfo CROSS JOIN LanguageIAF TranslationLang_ LEFT OUTER JOIN DTOInfoTranslation ON DTOInfo.DTOInfo_ID = DTOInfoTranslation.DTOInfo_ID AND TranslationLang_.LanguageIAF_ID = DTOInfoTranslation.LanguageIAF_ID WHERE DTOInfoTranslation_ID IS NULL ORDER BY DTOInfo.DTOInfo_DTOBaseName, TranslationLang_.LanguageIAF_ID"
    '        DirectCast(myMasterDB, ISQLServerNative).NativeExec(myNative)
    '    Catch ex As Exception
    '        MsgBox("Error creating the missing localized object entries (DTOInfoTranslation)! The message was: " + ex.Message)
    '    End Try
    '    'Create DTOFieldInfoTranslation entries (on the master-db)
    '    Try
    '        WriteInfo("Creating missing localized field entries")
    '        Dim myNative As String = "INSERT DTOFieldInfoTranslation (DTOFieldInfo.DTOFieldInfo_ID, TranslationLang_.LanguageIAF_ID, DTOFieldInfo_Label, DTOFieldInfo_Description) SELECT DTOFieldInfo.DTOFieldInfo_ID, TranslationLang_.LanguageIAF_ID, '[' + RTRIM(SUBSTRING(DTOFieldInfo.DTOFieldInfo_DTOFieldName,1,48)) + ']' as DTOFieldInfo_Label, '[Description of field ' + DTOInfo.DTOInfo_DTOBaseName  + '.' + DTOFieldInfo.DTOFieldInfo_DTOFieldName + ']' as DTOFieldInfo_Description FROM DTOFieldInfo INNER JOIN DTOInfo ON DTOFieldInfo.DTOInfo_ID = DTOInfo.DTOInfo_ID CROSS JOIN LanguageIAF TranslationLang_ LEFT OUTER JOIN DTOFieldInfoTranslation ON DTOFieldInfo.DTOFieldInfo_ID = DTOFieldInfoTranslation.DTOFieldInfo_ID AND TranslationLang_.LanguageIAF_ID = DTOFieldInfoTranslation.LanguageIAF_ID WHERE DTOFieldInfoTranslation_ID IS NULL ORDER BY DTOFieldInfo.DTOInfo_ID, DTOFieldInfo.DTOFieldInfo_ID, TranslationLang_.LanguageIAF_ID"
    '        DirectCast(myMasterDB, ISQLServerNative).NativeExec(myNative)
    '    Catch ex As Exception
    '        MsgBox("Error creating the missing localized field entries (DTOFieldInfoTranslation)! The message was: " + ex.Message)
    '    End Try
    '    'Add foreign key links (of the application-db into the master-db)
    '    Try
    '        WriteInfo("Updating foreign key links")
    '        Dim myNative As String = "SELECT INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME AS ForeignKey_TableName, INFORMATION_SCHEMA.KEY_COLUMN_USAGE.COLUMN_NAME AS ForeignKey_ColumnName, INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME AS PrimaryKey_TableName FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ON INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME = INFORMATION_SCHEMA.KEY_COLUMN_USAGE.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ON INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME"
    '        myDT = DirectCast(myApplicationDB, ISQLServerNative).NativeExec2DataTable(myNative)
    '        For Each myRow As DataRow In myDT.Rows
    '            Dim myForeignKey_TableName As String = myTranslator.ToString(myRow.Item("ForeignKey_TableName"))
    '            Dim myForeignKey_ColumnName As String = myTranslator.ToString(myRow.Item("ForeignKey_ColumnName"))
    '            Dim myPrimaryKey_TableName As String = myTranslator.ToString(myRow.Item("PrimaryKey_TableName"))
    '            Try
    '                LinkForeignKeyFieldInfo(myForeignKey_TableName, myForeignKey_ColumnName, myPrimaryKey_TableName, myApplicationId, myMasterDB)
    '            Catch ex As Exception
    '                MsgBox(String.Format("Could not update the link from column {0}.{1} to {2}! The message was: {3}", myForeignKey_TableName, myForeignKey_ColumnName, myPrimaryKey_TableName, ex.Message))
    '            End Try
    '        Next
    '    Catch ex As Exception
    '        MsgBox("Error getting the foreign key links (DTOFieldInfo)! The message was: " + ex.Message)
    '    End Try

    '    If (chkRecreateViews.Checked) Then
    '        'Get the tablebased DTOInfos (from the master-db)
    '        Dim myDTOInfoRows As New SortedDictionary(Of String, DataRow)(myComparer)
    '        Dim myDTOFieldInfoRows As New SortedDictionary(Of String, SortedDictionary(Of String, DataRow))(myComparer)
    '        InitDTOInfos(myApplicationId, myDTOInfoRows, myDTOFieldInfoRows, myExistingViewNames, myMasterDB, False)

    '        'Create views for DTOs (on the application-db)
    '        Dim myDTOViewDictionary As New Dictionary(Of Int32, SQL_CreateView)
    '        For Each myDTOInfo As KeyValuePair(Of String, DataRow) In myDTOInfoRows
    '            'todo: we do have a new property / Field on dtoinfo
    '            Dim myDTOInfoAutoMaintained As Boolean = myTranslator.ToBoolean(myDTOInfo.Value, "DTOInfo_AutoMaintained")
    '            If (Not myDTOInfoAutoMaintained) Then Continue For

    '            Dim myRow As DataRow = myDTOInfo.Value
    '            Dim myDTOInfoCreateDTOView As Boolean = myTranslator.ToBoolean(myRow, "DTOInfo_IsIdentifiable")
    '            If (Not myDTOInfoCreateDTOView) Then Continue For
    '            Dim myDTOInfoId As Int32 = myTranslator.ToInt32(myRow, "DTOInfo_ID")
    '            Dim myDTOInfoTableName As String = myTranslator.ToString(myRow, "DTOInfo_TableName")
    '            Dim myDTOInfoDTOBaseName As String = myTranslator.ToString(myRow, "DTOInfo_DTOBaseName")
    '            Dim myDTOInfoTranslationTableName As String = myTranslator.ToString(myRow, "DTOInfo_TranslationTableName")
    '            Dim myDTOViewName As String = GenerateDTOViewName(myDTOInfoDTOBaseName)
    '            'Get FieldInfo
    '            Dim myFieldInfo As SortedDictionary(Of String, DataRow) = Nothing
    '            If (myDTOFieldInfoRows.ContainsKey(myDTOInfoTableName)) Then
    '                myFieldInfo = myDTOFieldInfoRows.Item(myDTOInfoTableName)
    '            End If
    '            If (myFieldInfo Is Nothing) Then Continue For
    '            'Create Views
    '            Try
    '                WriteInfo(String.Format("Creating view {0}", myDTOViewName))
    '                CreateDTOView(myDTOViewName, myRow, myFieldInfo, myDTOViewDictionary, myApplicationId, myMasterDB, myApplicationDB)
    '            Catch ex As Exception
    '                MsgBox(String.Format("Could not create the DTO-view for table {0} on the application-db! The message was: {1}", myDTOInfoTableName, ex.Message))
    '                Continue For
    '            End Try
    '        Next
    '        'Update views (add foreign key reference to <View>.Object__Label of the primary key table's view)
    '        '            SELECT     dbo.DTOFieldInfo.DTOInfo_ID, dbo.DTOInfo.DTOInfo_TableName, dbo.DTOFieldInfo.DTOFieldInfo_TableColumnName, 
    '        '                      dbo.DTOFieldInfo.PrimaryKey_DTOInfo_ID, PrimaryKeyDTOInfo.DTOInfo_TableName AS PrimaryKey_DTOInfo_TableName, 
    '        '                      PrimaryKeyDTOInfo.DTOInfo_DTOViewName AS PrimaryKey_DTOInfo_DTOViewName, DTOFieldInfo_1.DTOFieldInfo_DTOFieldName
    '        'FROM         dbo.DTOFieldInfo INNER JOIN
    '        '                      dbo.DTOInfo ON dbo.DTOFieldInfo.DTOInfo_ID = dbo.DTOInfo.DTOInfo_ID INNER JOIN
    '        '                      dbo.DTOInfo AS PrimaryKeyDTOInfo ON dbo.DTOFieldInfo.PrimaryKey_DTOInfo_ID = PrimaryKeyDTOInfo.DTOInfo_ID INNER JOIN
    '        '                      dbo.DTOFieldInfo AS DTOFieldInfo_1 ON PrimaryKeyDTOInfo.DTOInfo_ID = DTOFieldInfo_1.DTOInfo_ID
    '        'WHERE     (PrimaryKeyDTOInfo.DTOInfo_DTOViewName IS NOT NULL) AND (DTOFieldInfo_1.DTOFieldInfo_IsPrimaryKey = 1) AND 
    '        '                      (DTOFieldInfo_1.DTOFieldInfo_IsMainField = 1)

    '        mySelect = New SQL_Select("DTOFieldInfo")
    '        mySelect.AddColumn("DTOFieldInfo.DTOInfo_ID", "DTOFieldInfo.PrimaryKey_DTOInfo_ID", "DTOInfo.DTOInfo_TableName", "DTOFieldInfo.DTOFieldInfo_TableColumnName", "DTOFieldInfo.PrimaryKey_DTOInfo_ID")
    '        mySelect.AddColumnWithAlias("PrimaryKeyDTOInfo", "DTOInfo_TableName", "PrimaryKey_DTOInfo_TableName")
    '        mySelect.AddColumnWithAlias("PrimaryKeyDTOInfo", "DTOInfo_DTOViewName", "PrimaryKey_DTOInfo_DTOViewName")
    '        mySelect.AddColumnWithAlias("PrimaryKeyDTOFieldInfo", "DTOFieldInfo_DTOFieldName", "PrimaryKey_DTOFieldInfo_DTOFieldName")
    '        mySelect.InnerJoin("DTOInfo", "DTOFieldInfo.DTOInfo_ID", "DTOInfo.DTOInfo_ID")
    '        mySelect.InnerJoin("DTOInfo", "PrimaryKeyDTOInfo", "DTOFieldInfo.PrimaryKey_DTOInfo_ID", "PrimaryKeyDTOInfo.DTOInfo_ID")
    '        mySelect.InnerJoin("DTOFieldInfo", "PrimaryKeyDTOFieldInfo", "PrimaryKeyDTOInfo.DTOInfo_ID", "PrimaryKeyDTOFieldInfo.DTOInfo_ID")
    '        myWhere1 = SQL_Where.ColumnIsNotNull("PrimaryKeyDTOInfo.DTOInfo_DTOViewName")
    '        myWhere2 = New SQL_Where("PrimaryKeyDTOFieldInfo.DTOFieldInfo_IsPrimaryKey", 1)
    '        myWhere3 = New SQL_Where("PrimaryKeyDTOFieldInfo.DTOFieldInfo_IsMainField", 1)
    '        mySelect.WhereAndWhere(myWhere1, myWhere2, myWhere3)
    '        myDT = myMasterDB.Exec2DataTable(mySelect)
    '        For Each myRow As DataRow In myDT.Rows
    '            Dim myForeignDTOInfoId As Int32 = myTranslator.ToInt32(myRow.Item("DTOInfo_ID"))
    '            Dim myPrimaryDTOInfoId As Int32 = myTranslator.ToInt32(myRow.Item("PrimaryKey_DTOInfo_ID"))
    '            Dim myForeignTableName As String = myTranslator.ToString(myRow.Item("DTOInfo_TableName"))
    '            'Get FieldInfo
    '            Dim myFieldInfo As SortedDictionary(Of String, DataRow) = Nothing
    '            If (myDTOFieldInfoRows.ContainsKey(myForeignTableName)) Then
    '                myFieldInfo = myDTOFieldInfoRows.Item(myForeignTableName)
    '            End If
    '            Dim myForeignColumnName As String = myTranslator.ToString(myRow.Item("DTOFieldInfo_TableColumnName"))
    '            Dim myPrimaryTableName As String = myTranslator.ToString(myRow.Item("PrimaryKey_DTOInfo_TableName"))
    '            Dim myPrimaryDTOViewName As String = myTranslator.ToString(myRow.Item("PrimaryKey_DTOInfo_DTOViewName"))
    '            Dim myPrimaryColumnName As String = myTranslator.ToString(myRow.Item("PrimaryKey_DTOFieldInfo_DTOFieldName"))
    '            Try
    '                WriteInfo(String.Format("Adding label of {0} as foreign label to {1}", myPrimaryTableName, myForeignTableName)) 'TODO: replace through view-names
    '                UpdateForeignKeyLabels(myDTOViewDictionary, myForeignDTOInfoId, myPrimaryDTOInfoId, myFieldInfo, myForeignTableName, myForeignColumnName, myPrimaryTableName, myPrimaryDTOViewName, myPrimaryColumnName, myApplicationDB, myMasterDB)
    '            Catch ex As Exception
    '                MsgBox(String.Format("Could not complete the DTOView with a primary key label of column ""{0}.{1}""! The message was: {2}", myForeignTableName, myForeignColumnName, ex.Message))
    '                Continue For
    '            End Try
    '        Next
    '        'Refresh the tablebased DTOInfos (from the master-db)
    '        myDTOInfoRows = New SortedDictionary(Of String, DataRow)(myComparer)
    '        myDTOFieldInfoRows = New SortedDictionary(Of String, SortedDictionary(Of String, DataRow))(myComparer)
    '        InitDTOInfos(myApplicationId, myDTOInfoRows, myDTOFieldInfoRows, myExistingViewNames, myMasterDB, True)
    '        'Update views (generate Object__Label)
    '        mySelect = New SQL_Select("DTOFieldInfo")
    '        mySelect.InnerJoin("DTOInfo", "DTOFieldInfo.DTOInfo_ID", "DTOInfo.DTOInfo_ID")
    '        mySelect.AddColumn("DTOFieldInfo.DTOInfo_ID", "DTOInfo_TableName", "DTOInfo_DTOViewName")
    '        mySelect.Distinct = True
    '        mySelect.WhereAndWhere(SQL_Where.ColumnIsNotNull("DTOFieldInfo_LabelColumnSegment"), New SQL_Where("DTOInfo.Application_ID", myApplicationId))
    '        mySelect.OrderBy("DTOInfo_TableName")
    '        myDT = myMasterDB.Exec2DataTable(mySelect)
    '        For Each myRow As DataRow In myDT.Rows
    '            If (myRow Is Nothing) Then Continue For
    '            Dim myTableName As String = myTranslator.ToString(myRow, "DTOInfo_TableName")
    '            Dim myViewName As String = myTranslator.ToString(myRow, "DTOInfo_DTOViewName")
    '            WriteInfo(String.Format("Updating label of {0}", myViewName))
    '            Dim myDTOInfoId As Int32 = myTranslator.ToInt32(myRow, "DTOInfo_ID")
    '            If (myTableName Is Nothing) OrElse (myViewName Is Nothing) OrElse (Not myDTOViewDictionary.ContainsKey(myDTOInfoId)) Then Continue For
    '            If (Not myDTOFieldInfoRows.ContainsKey(myTableName)) Then Continue For
    '            Dim myFieldInfo As SortedDictionary(Of String, DataRow) = myDTOFieldInfoRows.Item(myTableName)
    '            Try
    '                UpdateObjectLabel(myDTOViewDictionary.Item(myDTOInfoId), myFieldInfo, myApplicationDB, myMasterDB)
    '            Catch ex As Exception
    '                MsgBox(String.Format("Could not adjust the Object__Label of DTOView ""{0}""! The message was: {1}", myViewName, ex.Message))
    '                Continue For
    '            End Try
    '        Next

    '    End If

    '    'update DTOInfoImages
    '    UpdateImages(myMasterDB)

    '    'create all images
    '    CreateDefaultImagesForDTOInfoImages(myMasterDB)

    '    'Finished
    '    WriteInfo("Finished")
    'End Sub

End Module
